package com.hr.rbac.dao.Impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;

import com.hr.rbac.dao.RoleDao;
import com.hr.rbac.domain.Role;
import com.hr.utils.PageBean;
import com.hr.utils.TxQueryRunner;

/**
 * 角色实现层
 * @author 杨谦
 * @date 2015-11-5
 *
 */
public class RoleDaoImpl implements RoleDao {
	private QueryRunner qr = new TxQueryRunner();
	private PrivilegeDaoImpl privilegeDaoImpl = new PrivilegeDaoImpl(); 
	
	@Override
	public Role findByRoleUserId(String userID) {
		// TODO Auto-generated method stub
		String role_Id;
		try {
			String sql = "select role_ID from ROLE_USER where user_ID = ?";
			role_Id = (String)qr.query(sql, new ScalarHandler(),userID);
			if(role_Id != null){
				return findByRoleId(role_Id);
			} else {
				return null;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public Role findByRoleId(String roleID) {
		// TODO Auto-generated method stub
		String sql = "select * from HR_ROLE where role_ID = ?";
		try {
			if(qr.query(sql, new BeanHandler<Role>(Role.class), roleID) != null){
				return qr.query(sql, new BeanHandler<Role>(Role.class), roleID);
			} else {
				return null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public List<Role> QueryAll() {
		try {
			String sql = "select * from HR_ROLE";
			return qr.query(sql, new BeanListHandler<Role>(Role.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	@Test
	public void test() {
		System.out.println(QueryAll());
	}
	
	@Override
	public PageBean<Role> queryAll_Page(int PageCurrent,int PageSize) {
		// TODO Auto-generated method stub
		try {
			PageBean<Role> pb = new PageBean<Role>();
			//设置当前页面页码
			pb.setPageCurrent(PageCurrent);
			//设置当前页面的页面容量
			pb.setPageSize(PageSize);
			String sql = "select Count(*) from HR_ROLE";
			Number num = (Number)qr.query(sql, new ScalarHandler());
			int rowCount = num.intValue();//获取数据库中的所有记录数
			pb.setRowCount(rowCount);
			sql = "select * from (select a1.*,rownum rn from (select * from HR_ROLE)a1 where rownum<="+PageCurrent*PageSize+")where rn >="+((PageCurrent-1)*PageSize+1);
			List<Role> list_role = qr.query(sql, new BeanListHandler<Role>(Role.class));
			pb.setBeanList(list_role);
			return pb;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

	@Override
	public void addRole(Role role) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Role findByRoleName(String roleName) {
		String sql = "select * from HR_ROLE where role_Name = ?";
		try {
			return qr.query(sql, new BeanHandler<Role>(Role.class), roleName);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public void saveRole(String privilege_id, String function_id,String role_id) {
		try {
			String sql = "insert into FUNCTION_PRIVILEGE values(?,?,?)";
			Object[] params = {privilege_id,function_id,role_id};
			qr.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void saveRole_Privilege(String privilege_Id, String role_ID) {
		// TODO Auto-generated method stub
		try {
			String sql = "insert into PRIVILEGE_ROLE values(?,?)";
			Object[] params = {privilege_Id,role_ID};
			qr.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void save(Role role) {
		// TODO Auto-generated method stub
		try {
			String sql = "insert into HR_ROLE values(?,?)";
			Object[] params = {role.getRole_ID(),role.getRole_Name()};
			qr.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void saveRoleAndUser(String user_id, String role_id) {
		// TODO Auto-generated method stub
		try {
			String sql = "insert into ROLE_USER values(?,?)";
			Object[] params = {user_id,role_id};
			qr.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void updateRoleAndUser(String user_id, String role_id) {
		// TODO Auto-generated method stub
		try {
			String sql = "update ROLE_USER set ROLE_ID = ? where USER_ID = ?";
			Object[] params = {role_id,user_id};
			qr.update(sql,params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void deleteRoleByRoleID(String role_id) {
		// TODO Auto-generated method stub
		try {
			String sql = "delete HR_ROLE where ROLE_ID = ?";
			qr.update(sql, role_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void updateRoleByRoleID(String role_id) {
		// TODO Auto-generated method stub
		String sql = "update PRIVILEGE_ROLE set  where";
	}


}
